package com.client.koshercook.client;

import com.google.gwt.event.dom.client.ClickEvent;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.i18n.client.HasDirection.Direction;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
/** send mail GUI */
public class SendMailBox extends Composite{

	private PushButton open = new PushButton("שלח לחבר");
	private Button ok = new Button("אישור");
	private Button cancel = new Button("ביטול");
	private DialogBox dBox = new DialogBox();
	private TextBox desMailBox = new TextBox(); //designated nail address
	
	private VerticalPanel VPanel = new VerticalPanel();
	private HorizontalPanel HPanel = new HorizontalPanel();
	private HorizontalPanel desMailPanel = new HorizontalPanel();
	
	private String content = null;
	private String subject = null;
	private TextArea commentTextArea = new TextArea(); //save user comments
	private Label mailText = new Label(":אל");
	private Label contentText = new Label("הוסף תוכן");
	
	public SendMailBox(){
			
		commentTextArea.addKeyUpHandler(new KeyUpHandler(){
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ESCAPE) {
					dBox.hide();
					commentTextArea.setFocus(false);
				}
			}
		});
		
		desMailBox.addKeyUpHandler(new KeyUpHandler(){
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ESCAPE) {
					dBox.hide();
					commentTextArea.setFocus(false);
				}
			}
		});
		
		open.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if(!Client.model.isConnected()){
					//Window.alert("עליך להיות מחובר בכדי לשלוח מייל");
					Client.model.setInfoTextAndPO("עליך להיות מחובר בכדי לשלוח מייל");
				}else{
					
					dBox.setPopupPosition(open.getAbsoluteLeft(), open.getAbsoluteTop());
					dBox.center();
					dBox.show();
					desMailBox.setFocus(true);
				}
			}
		});
		
		ok.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				String desMail = desMailBox.getText();
				if(desMail.equals("")){
					Window.alert("עליך למלא את הכתובת אליה ישלח המייל");
					return;
				}
				Client.model.sendMail(desMail, subject + "@" + commentTextArea.getText() +  "@" + content);
				clear();
			}
		});

		cancel.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				clear();
			}
		});
		
		dBox.setText("הכנס כתובת אי-מייל של הנמען");
		
		commentTextArea.setDirection(Direction.RTL);
		commentTextArea.setCharacterWidth(60);
		commentTextArea.setVisibleLines(3);
		desMailBox.setDirection(Direction.LTR);
		desMailBox.setWidth("360px");
		dBox.add(VPanel);
		VPanel.add(desMailPanel);
		VPanel.add(contentText);
		VPanel.add(commentTextArea);
		VPanel.add(HPanel);
		VPanel.setSpacing(2);
		VPanel.setCellHorizontalAlignment(desMailPanel,HasHorizontalAlignment.ALIGN_CENTER);		
		VPanel.setCellHorizontalAlignment(commentTextArea, HasAlignment.ALIGN_CENTER);
		VPanel.setCellHorizontalAlignment(HPanel, HasAlignment.ALIGN_CENTER);		
		VPanel.setCellHorizontalAlignment(contentText, HasAlignment.ALIGN_RIGHT);
		HPanel.add(cancel);
		HPanel.add(ok);
		HPanel.setSpacing(5);
		desMailPanel.add(desMailBox);
		desMailPanel.add(mailText);
		desMailPanel.setCellHorizontalAlignment(mailText, HasHorizontalAlignment.ALIGN_RIGHT);
		desMailPanel.setCellHorizontalAlignment(desMailBox, HasHorizontalAlignment.ALIGN_LEFT);
		desMailPanel.setSpacing(20);
		initWidget(open);
	}
	
	private void clear(){
		dBox.hide();
		commentTextArea.setText("");
		desMailBox.setText("");
	}
	
	public void setContent(String content){
		this.content = content;
	}
	
	public void setSubject(String content){
		this.subject = content;
	}
	
	public void disable(){
		open.setEnabled(false);
	}
	
	public void enable(){
		open.setEnabled(true);
	}
}
